This is the second part of a two-part vignette designed to collect and analyze Twitter posts from members of the US Congress and the US President. This part of the vignette carries out the database initialization and initial status collections. This part of the vignette consists of two sub-parts: (a) updating the twitter database with new posts, and (b) analysis. These two sub-parts can be run together or independently, as often as required.
Very Important Note: As with the first part of this vignette, Twitter data collection can take a very long time. In this part of the vignette, collection is carried out using the update_user_timelines function. If the Twitter database has been kept up to date, i.e., if this function has been called recently on the Twitter data connection, then there will not be many new statuses and the method will run in a few minutes. However, if this function has not been called for a longer period of time, and the database consists of all members of congress, then there will be more new statuses to collect and the function will take longer to complete.
We begin by allocating additional memory for RJava to support sentiment analysis, loading the twinfoR namespace, establishing the Twitter database connection to the sqlite database created in Part I of this vignette, and loading the authentication vector. See (and execute) the us_politics_1 vignette if you haven’t already done so.
This code assumes the auth.vector from Part I is saved. If it isn’t, reauthenticate using the authorize_IT() function as done in Part I.
This function should take considerably less time to run than the initial call, because it only collects new statuses. Again, we divert output to the NULL device.
# sink("/dev/null") # Recommended to suppress Rsentiment output, but
## might not work on all platforms.
update_user_timelines(
con,
calc.Rsentiment = TRUE,
calc.syu = TRUE
)
# sink()# Summarize database
summarize_database(con)
#> Query users: 558
#> User categories:
#> party
#> body
#> since_id
#> friends_collected
#> followers_collected
#> No query text table.
#> Users: 579774
#> Statuses: 1599290
#> Unique user mentions: 123710
#> Unique hashtags: 81459
#> Unique URLs: 521248
#> Total Images: 532138
#> Unique Images: 497016
#> Total relationships: 1095918
#> Database file size: 1390MBThis section goes quickly through the analysis capability available in the twinfoR package.
topusermentions.dem <- top_usermentions(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'democrat'"
)
topusermentions.rep <- top_usermentions(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'republican'"
)
knitr::kable(
data.frame(
Screen_Name = paste("@",topusermentions.dem$user_mention_screen_name,sep=""),
Name = topusermentions.dem$user_mention_name,
Mentions = topusermentions.dem$n,
stringsAsFactors = FALSE
),
caption = "Democrat User Mentions"
)| Screen_Name | Name | Mentions |
|---|---|---|
| @realDonaldTrump | Donald J. Trump | 281 |
| @senatemajldr | Leader McConnell | 171 |
| @HouseDemocrats | House Democrats | 102 |
| @POTUS | President Trump | 82 |
| @EPA | U.S. EPA | 81 |
| @AppropsDems | House Appropriations | 67 |
| @SpeakerPelosi | Nancy Pelosi | 63 |
| @SenateGOP | Senate Republicans | 58 |
| @BobbyScott | Rep. Bobby Scott | 53 |
| @SenateDems | Senate Democrats | 39 |
knitr::kable(
data.frame(
Screen_Name = paste("@",topusermentions.rep$user_mention_screen_name,sep=""),
Name = topusermentions.rep$user_mention_name,
Mentions = topusermentions.rep$n,
stringsAsFactors = FALSE
),
caption = "Republican User Mentions"
)| Screen_Name | Name | Mentions |
|---|---|---|
| @realDonaldTrump | Donald J. Trump | 138 |
| @POTUS | President Trump | 88 |
| @HouseGOP | House Republicans | 31 |
| @ChuckGrassley | ChuckGrassley | 26 |
| @WhiteHouse | The White House | 26 |
| @SpeakerPelosi | Nancy Pelosi | 25 |
| @senatemajldr | Leader McConnell | 24 |
| @RepSamGraves | Rep. Sam Graves | 24 |
| @JohnCornyn | Senator John Cornyn | 21 |
| @SenTedCruz | Senator Ted Cruz | 19 |
topurls.dem <- top_urls(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'democrat'"
)
topurls.rep <- top_urls(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'republican'"
)
knitr::kable(
list(
Democrat = data.frame(
Dem.URL = topurls.dem$extended_url,
Count = topurls.dem$n,
stringsAsFactors = FALSE
),
Republican = data.frame(
Rep.URL = topurls.rep$extended_url,
Count = topurls.rep$n,
stringsAsFactors = FALSE
)
),
caption = "Top Democrat and Republican URLs"
)topmedia.dem <- top_media(
con,
start.date = Sys.Date()-14,
where.criteria = "query_users.party = 'democrat'",
save.to.file = FALSE
)
topmedia.rep <- top_media(
con,
start.date = Sys.Date()-14,
where.criteria = "query_users.party = 'republican'",
save.to.file = FALSE
)
knitr::kable(
list(
data.frame(
Dem.image_url = topmedia.dem$media_url,
count = topmedia.dem$n,
stringsAsFactors = FALSE
),
data.frame(
Rep.image_url = topmedia.rep$media_url,
count = topmedia.rep$n,
stringsAsFactors = FALSE
)
),
caption = "Top Media Posted by Democrats and Republicans"
)
|
|
## Top Dem Image (Not Run)
# status_media(
# topmedia.dem[1,],
# con,
# update.media.b64 = FALSE,
# display.image = TRUE
# )
## Top Rep Image (Not Run)
# status_media(
# topmedia.rep[1,],
# con,
# update.media.b64 = FALSE,
# display.image = TRUE
# )|
|
|
| Top Democrat Image | Top Republican Image |
The top_media and status_media have many options for saving and rendering images. See documentation for details.
toptweeters.dem <- top_tweeters(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'democrat'"
)
toptweeters.rep <- top_tweeters(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'republican'"
)
toptweeters.dem$description <- gsub("\n"," -- ",toptweeters.dem$description)
toptweeters.dem$location <- gsub("\n"," -- ",toptweeters.dem$location)
toptweeters.rep$description <- gsub("\n"," -- ",toptweeters.rep$description)
toptweeters.rep$location <- gsub("\n"," -- ",toptweeters.rep$location)
knitr::kable(
data.frame(
Name = toptweeters.dem$name,
Location = toptweeters.dem$location,
Description = toptweeters.dem$description,
Tweets = toptweeters.dem$n,
stringsAsFactors = FALSE
),
caption = "Top Democrat Tweeters"
)| Name | Location | Description | Tweets |
|---|---|---|---|
| Rep. Veronica Escobar | El Paso, TX | Tejana, mother & public servant. – Proudly representing Texas’ 16th Congressional District in the U.S. House of Representatives. | 132 |
| Rep. Don Beyer | Northern Virginia & DC | Representing Northern Virginia’s #VA8 in Congress. Vice-Ranking @SciCmteDems, top Science Oversight Dem. Co-Chair @CaucusOnClimate. #StopGunViolence #EqualPay | 132 |
| EPW Democrats | News from the minority office of the U.S. Senate Committee on Environment and Public Works, led by Ranking Member @SenatorCarper | 112 | |
| Senator Patty Murray | WA & DC | Official account of U.S. Senator Patty Murray (D-WA) | Tweets come from staff unless signed “-PM” by Senator Murray | RT≠endorsement | 108 |
| Rep. Pramila Jayapal | Seattle, WA | Congresswoman & lifelong organizer. Proudly serving WA-07. Co-Chair of @USProgressives, Senior Whip, Budget Vice Ranking. https://t.co/d22w0Bpl8R | 102 |
| Sheldon Whitehouse | Rhode Island | U.S. Senator from #RhodeIsland, the Ocean State. RT≠endorsement. #TimeToWakeUp | 99 |
| Rep. Joe Neguse | Lafayette, Colorado | Representative for Colorado’s 2nd Congressional District. Official Account. | 88 |
| Senator Cortez Masto | U.S. Senator from the great state of Nevada. | 80 | |
| Rep. Val Demings | Washington, DC I Orlando, FL | U.S. Representative Florida’s 10th Congressional District. Public Servant. Former Orlando Police Chief. Christian. Wife. Mother. Grandmother. Harley Enthusiast. | 78 |
| Congresswoman Mary Gay Scanlon | Congresswoman from Pennsylvania’s 7th Congressional District. | 74 |
knitr::kable(
data.frame(
Name = toptweeters.rep$name,
Location = toptweeters.rep$location,
Description = toptweeters.rep$description,
Tweets = toptweeters.rep$n,
stringsAsFactors = FALSE
),
caption = "Top Republican Tweeters"
)| Name | Location | Description | Tweets |
|---|---|---|---|
| Senator John Cornyn | Austin, Texas | Personally curated news I find interesting, pics, and opinions. Honored to represent the great state of Texas in the US Senate. RTs =/= endorsements | 94 |
| Rep. Bradley Byrne | Southwest Alabama | I am proud to represent Alabama’s First Congressional District in the U.S. House of Representatives. Official Congressional Account | 78 |
| Senator Ted Cruz | Houston, Texas | Representing the State of Texas in the United States Senate. | 74 |
| President Trump | Washington, D.C. | 45th President of the United States of America, @realDonaldTrump. Tweets archived: https://t.co/eVVzoBb3Zr | 55 |
| Rob Portman | Ohio | United States Senator from the Buckeye State. | 52 |
| Senator Rubio Press | Florida, USA | Official account of U.S. Senator @MarcoRubio’s PressShop. For more information please visit his website. | 46 |
| Senator Jerry Moran | Kansas | Official account of U.S. Senator Jerry Moran. Honored to represent Kansans in the U.S. Senate. | 39 |
| Sen. Lamar Alexander | Maryville, TN | 7th generation Tennessean born and raised in Maryville #LowerHealthCareCosts #OpioidCrisisResponse #MusicModernizationAct #RestoreOurParksAct | 38 |
| Ralph Norman | Rock Hill, SC | Official account of Congressman Ralph Norman. Proudly representing the 5th District of South Carolina. | 37 |
| Sean Duffy | Devoted husband of one, proud father of eight, and the Representative for Wisconsin’s Seventh District. Also at https://t.co/Y7pV5IPxzR. #GoPackGo | 36 |
mostliked.dem <- most_liked(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'democrat'"
)
mostliked.rep <- most_liked(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'republican'"
)
# For nice Markdown rendering
mostliked.dem$description <- gsub("\n"," -- ",mostliked.dem$description)
mostliked.dem$location <- gsub("\n"," -- ",mostliked.dem$location)
mostliked.dem$text <- gsub("\n"," -- ",mostliked.dem$text)
mostliked.dem$name <- gsub("\n"," -- ",mostliked.dem$name)
mostliked.rep$description <- gsub("\n"," -- ",mostliked.rep$description)
mostliked.rep$location <- gsub("\n"," -- ",mostliked.rep$location)
mostliked.rep$text <- gsub("\n"," -- ",mostliked.rep$text)
mostliked.rep$name <- gsub("\n"," -- ",mostliked.rep$name)
knitr::kable(
data.frame(
Name = mostliked.dem$name,
Created = mostliked.dem$created_at,
Likes = mostliked.dem$favorite_count,
Text = mostliked.dem$text,
stringsAsFactors = FALSE
),
caption = "Most Liked Democrat Tweets"
)| Name | Created | Likes | Text |
|---|---|---|---|
| Maxine Waters | 2019-01-14 03:51:11 | 93297 | Trump, you didn’t get $5 billion to carry out your ill advised election promise & now you want to declare an emergency. The only emergency facing our country is the need to impeach you before you destroy our country, our ideals, & leadership in the world. Trump, go home. Bye bye! |
| Adam Schiff | 2019-01-13 18:47:19 | 77350 | GOP controlled the House and Senate for two years under Trump. If the wall was such an emergency, they could have used reconciliation to pass it with 50 votes in Senate. Why didn’t they? – – I guess throwing millions off health care & cutting corporate taxes were higher priorities. |
| Rep. Eric Swalwell | 2019-01-11 00:53:11 | 72939 | Gun violence is a national emergency |
| Adam Schiff | 2019-01-16 01:42:24 | 72657 | Call me old fashioned, but America deserves an Attorney General committed to following the advice of ethics lawyers. – – Call me naive, but there are attorneys qualified to serve as AG who have not expressed hostility to the Mueller probe. – – Call me what you will, but demand better. |
| Rep. Eric Swalwell | 2019-01-15 04:58:20 | 69753 | I don’t give two french fries about what @realDonaldTrump serves at the @WhiteHouse. But we should all care about what food is on the tables of America’s 800,000 federal workers who aren’t being paid right now. #TrumpShutdown |
| Nancy Pelosi | 2019-01-16 14:50:50 | 69133 | Today, I wrote to @realDonaldTrump recommending that we delay the State of the Union until after government re-opens, as the @SecretService, the lead federal agency for #SOTU security, faces its 26th day without funding. https://t.co/K2oL8WGvqo https://t.co/g3fIlxDbbK |
| Rep. Eric Swalwell | 2019-01-13 21:56:00 | 62784 | Six months ago, @RepAdamSchiff and I tried to subpoena the interpreter from the Trump-Putin meeting. The GOP blocked us. We knew then something was fishy. We now know Trump took the notes. Lost time and more damage to our democracy is the cost of GOP obstruction. https://t.co/o6eSXeb6vf |
| Rep. Eric Swalwell | 2019-01-13 00:02:19 | 60179 | Destruction of evidence is consciousness of guilt. At this point please show me evidence that @realDonaldTrump is not working for Russia. https://t.co/t3DOGmNVv8 |
| Adam Schiff | 2019-01-13 15:02:57 | 56825 | Last year, we sought to obtain the interpreter’s notes or testimony, from the private meeting between Trump and Putin. The Republicans on our committee voted us down. Will they join us now? Shouldn’t we find out whether our president is really putting “America first?” https://t.co/hzgfTRl653 |
| Mark Warner | 2019-01-11 01:39:56 | 55784 | I think we’ve had enough of the President running the government like one of his businesses. |
knitr::kable(
data.frame(
Name = mostliked.rep$name,
Created = mostliked.rep$created_at,
Likes = mostliked.rep$favorite_count,
Text = mostliked.rep$text,
stringsAsFactors = FALSE
),
caption = "Most Liked Republican Tweets"
)| Name | Created | Likes | Text |
|---|---|---|---|
| Senator Rand Paul | 2019-01-17 13:30:26 | 65178 | Senator McConnell is in charge of the Senate. If Mrs. Pelosi refuses to allow the president to deliver the State of the Union in the House, I propose we move it to the Senate and make it happen! |
| Senator Rand Paul | 2019-01-16 23:00:46 | 52315 | I have never been prouder of President Donald Trump. In today’s meeting, he stood up for a strong America and steadfastly opposed foreign wars. Putting America First means declaring victory in Afghanistan and Syria. President Trump is delivering on his promises. |
| Senator Rand Paul | 2019-01-15 22:18:54 | 51471 | I agree with @realDonaldTrump! It’s time to declare victory in Afghanistan and bring our troops home. We spend $46 BILLION a year in Afghanistan! How about we take that money and build roads and bridges and strengthen border security here in America? #AmericaFirst |
| Ron DeSantis | 2019-01-11 22:04:52 | 49700 | Effective immediately, I am officially suspending Broward County Sheriff Scott Israel for his repeated failures, incompetence and neglect of duty. https://t.co/tkHzxTHhjH |
| Rep. Jim Jordan | 2019-01-16 13:42:59 | 46788 | Here’s why we can’t get a deal on securing the border and ending the shutdown: today’s Left has taken the most radical positions in American history. https://t.co/UG8P5efPfb |
| Rep. Jim Jordan | 2019-01-15 18:58:40 | 45207 | In case you needed MORE proof that Democrats are more interested in stopping the President than helping the country… – – They rejected President Trump’s invitation to meet at the White House. They refuse to come to a compromise to secure the border and end the shutdown. |
| Senator Rand Paul | 2019-01-16 18:10:26 | 42397 | Sunnis have been killing Shia since the massacre at Karbala in 680 AD. If we wait until they stop killing each other, we will stay for a thousand years or more. I agree with @realDonaldTrump. Bring the troops home. |
| Mark Meadows | 2019-01-15 17:30:34 | 32644 | House Democrats just rejected an invitation from President Trump to come to the White House for a bipartisan discussion on border security solutions. – – And they expect voters to believe they’re serious about negotiating to find a compromise? Americans aren’t buying it. |
| Rep. Jim Jordan | 2019-01-13 16:58:31 | 32205 | Judiciary Chairman @RepJerryNadler: “There is no reason to doubt the seriousness or professionalism of the FBI” – – Really? No reason to doubt? – – -Andy McCabe fired for serial lying – -Lisa Page demoted/left FBI for extreme animus against @POTUS – -Peter Strzok demoted/fired, same reason |
| Mark Meadows | 2019-01-14 14:42:46 | 30706 | The NYT piece on the FBI investigating Trump was intended to absolve FBI leadership. It actually does the opposite. It reveals FBI chose to investigate POTUS to retaliate against him for firing Comey. They NEVER had any real evidence but opened an investigation anyway. Unreal. |
mostretweeted.dem <- most_retweeted(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'democrat'"
)
mostretweeted.rep <- most_retweeted(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'republican'"
)
# For nice Markdown rendering
mostretweeted.dem$description <- gsub("\n"," -- ",mostretweeted.dem$description)
mostretweeted.dem$location <- gsub("\n"," -- ",mostretweeted.dem$location)
mostretweeted.dem$text <- gsub("\n"," -- ",mostretweeted.dem$text)
mostretweeted.dem$name <- gsub("\n"," -- ",mostretweeted.dem$name)
mostretweeted.rep$description <- gsub("\n"," -- ",mostretweeted.rep$description)
mostretweeted.rep$location <- gsub("\n"," -- ",mostretweeted.rep$location)
mostretweeted.rep$text <- gsub("\n"," -- ",mostretweeted.rep$text)
mostretweeted.rep$name <- gsub("\n"," -- ",mostretweeted.rep$name)
knitr::kable(
data.frame(
Name = mostretweeted.dem$name,
Created = mostretweeted.dem$created_at,
Retweet_Count = mostretweeted.dem$retweet_count,
Text = mostretweeted.dem$text,
stringsAsFactors = FALSE
),
caption = "Most Retweeted Democrat Tweets"
)| Name | Created | Retweet_Count | Text |
|---|---|---|---|
| Adam Schiff | 2019-01-13 18:47:19 | 27854 | GOP controlled the House and Senate for two years under Trump. If the wall was such an emergency, they could have used reconciliation to pass it with 50 votes in Senate. Why didn’t they? – – I guess throwing millions off health care & cutting corporate taxes were higher priorities. |
| Maxine Waters | 2019-01-14 03:51:11 | 23857 | Trump, you didn’t get $5 billion to carry out your ill advised election promise & now you want to declare an emergency. The only emergency facing our country is the need to impeach you before you destroy our country, our ideals, & leadership in the world. Trump, go home. Bye bye! |
| Rep. Eric Swalwell | 2019-01-13 21:56:00 | 23289 | Six months ago, @RepAdamSchiff and I tried to subpoena the interpreter from the Trump-Putin meeting. The GOP blocked us. We knew then something was fishy. We now know Trump took the notes. Lost time and more damage to our democracy is the cost of GOP obstruction. https://t.co/o6eSXeb6vf |
| Rep. Eric Swalwell | 2019-01-13 00:02:19 | 21085 | Destruction of evidence is consciousness of guilt. At this point please show me evidence that @realDonaldTrump is not working for Russia. https://t.co/t3DOGmNVv8 |
| Rep. Susie Lee | 2019-01-17 13:27:20 | 19602 | My fellow new Members & I were elected to work. But @SenateMajLdr McConnell seems to think otherwise. After 27 days, he still hasn’t allowed a single vote in the Senate on multiple House plans to re-open government. RT if you think he’s wrong! #EndTheShutdown #WheresMitch |
| Adam Schiff | 2019-01-16 01:42:24 | 19602 | Call me old fashioned, but America deserves an Attorney General committed to following the advice of ethics lawyers. – – Call me naive, but there are attorneys qualified to serve as AG who have not expressed hostility to the Mueller probe. – – Call me what you will, but demand better. |
| Adam Schiff | 2019-01-13 15:02:57 | 19293 | Last year, we sought to obtain the interpreter’s notes or testimony, from the private meeting between Trump and Putin. The Republicans on our committee voted us down. Will they join us now? Shouldn’t we find out whether our president is really putting “America first?” https://t.co/hzgfTRl653 |
| Kirsten Gillibrand | 2019-01-17 18:40:54 | 18588 | .@senatemajldr has scheduled a vote today. No, it’s not to reopen the government and begin paying 800,000 federal workers who’ve gone without pay for 27 days. It’s to restrict reproductive health care coverage. Unbelievable. – – Stop attacking women. Open the government. |
| Nancy Pelosi | 2019-01-16 14:50:50 | 17917 | Today, I wrote to @realDonaldTrump recommending that we delay the State of the Union until after government re-opens, as the @SecretService, the lead federal agency for #SOTU security, faces its 26th day without funding. https://t.co/K2oL8WGvqo https://t.co/g3fIlxDbbK |
| Rep. Eric Swalwell | 2019-01-11 00:53:11 | 16819 | Gun violence is a national emergency |
knitr::kable(
data.frame(
Name = mostretweeted.rep$name,
Created = mostretweeted.rep$created_at,
Retweet_Count = mostretweeted.rep$retweet_count,
Text = mostretweeted.rep$text,
stringsAsFactors = FALSE
),
caption = "Most Retweeted Republican Tweets"
)| Name | Created | Retweet_Count | Text |
|---|---|---|---|
| Rep. Jim Jordan | 2019-01-16 13:42:59 | 20236 | Here’s why we can’t get a deal on securing the border and ending the shutdown: today’s Left has taken the most radical positions in American history. https://t.co/UG8P5efPfb |
| Rep. Jim Jordan | 2019-01-15 18:58:40 | 19991 | In case you needed MORE proof that Democrats are more interested in stopping the President than helping the country… – – They rejected President Trump’s invitation to meet at the White House. They refuse to come to a compromise to secure the border and end the shutdown. |
| Senator Rand Paul | 2019-01-17 13:30:26 | 19784 | Senator McConnell is in charge of the Senate. If Mrs. Pelosi refuses to allow the president to deliver the State of the Union in the House, I propose we move it to the Senate and make it happen! |
| Senator Rand Paul | 2019-01-15 22:18:54 | 15587 | I agree with @realDonaldTrump! It’s time to declare victory in Afghanistan and bring our troops home. We spend $46 BILLION a year in Afghanistan! How about we take that money and build roads and bridges and strengthen border security here in America? #AmericaFirst |
| Senator Rand Paul | 2019-01-16 23:00:46 | 15231 | I have never been prouder of President Donald Trump. In today’s meeting, he stood up for a strong America and steadfastly opposed foreign wars. Putting America First means declaring victory in Afghanistan and Syria. President Trump is delivering on his promises. |
| Mark Meadows | 2019-01-15 17:30:34 | 14660 | House Democrats just rejected an invitation from President Trump to come to the White House for a bipartisan discussion on border security solutions. – – And they expect voters to believe they’re serious about negotiating to find a compromise? Americans aren’t buying it. |
| Ron DeSantis | 2019-01-11 22:04:52 | 14099 | Effective immediately, I am officially suspending Broward County Sheriff Scott Israel for his repeated failures, incompetence and neglect of duty. https://t.co/tkHzxTHhjH |
| Senator Rand Paul | 2019-01-16 18:10:26 | 13792 | Sunnis have been killing Shia since the massacre at Karbala in 680 AD. If we wait until they stop killing each other, we will stay for a thousand years or more. I agree with @realDonaldTrump. Bring the troops home. |
| Rep. Jim Jordan | 2019-01-13 16:58:31 | 13290 | Judiciary Chairman @RepJerryNadler: “There is no reason to doubt the seriousness or professionalism of the FBI” – – Really? No reason to doubt? – – -Andy McCabe fired for serial lying – -Lisa Page demoted/left FBI for extreme animus against @POTUS – -Peter Strzok demoted/fired, same reason |
| Mark Meadows | 2019-01-14 14:42:46 | 13252 | The NYT piece on the FBI investigating Trump was intended to absolve FBI leadership. It actually does the opposite. It reveals FBI chose to investigate POTUS to retaliate against him for firing Comey. They NEVER had any real evidence but opened an investigation anyway. Unreal. |
mostpopRT.dem <- most_popular_RT_in_sample(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'democrat'"
)
mostpopRT.rep <- most_popular_RT_in_sample(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'republican'"
)
# For nice Markdown rendering
mostpopRT.dem$description <- gsub("\n"," -- ",mostpopRT.dem$description)
mostpopRT.dem$location <- gsub("\n"," -- ",mostpopRT.dem$location)
mostpopRT.dem$text <- gsub("\n"," -- ",mostpopRT.dem$text)
mostpopRT.dem$name <- gsub("\n"," -- ",mostpopRT.dem$name)
mostpopRT.rep$description <- gsub("\n"," -- ",mostpopRT.rep$description)
mostpopRT.rep$location <- gsub("\n"," -- ",mostpopRT.rep$location)
mostpopRT.rep$text <- gsub("\n"," -- ",mostpopRT.rep$text)
mostpopRT.rep$name <- gsub("\n"," -- ",mostpopRT.rep$name)
knitr::kable(
data.frame(
Name = mostpopRT.dem$name,
Created = mostpopRT.dem$created_at,
Dem_Retweets = mostpopRT.dem$n,
Text = mostpopRT.dem$text,
stringsAsFactors = FALSE
),
caption = "Most Popular Retweets Among Democrats"
)| Name | Created | Dem_Retweets | Text |
|---|---|---|---|
| Rep. Diana DeGette | 2019-01-14 14:19:49 | 27 | Stories like Mallory’s reveal the terrifying reality of this shutdown. Furloughed workers are facing life-or-death situations, just so @realDonaldTrump can try to force taxpayers to fund a border wall he promised Mexico would pay for. #EndTheShutdown https://t.co/kbEth0RIRP |
| Rep. Bobby Scott | 2019-01-16 12:12:09 | 24 | There is no place in America where someone working full time & making the $7.25 federal minimum wage can make ends meet. Today, House & Senate Democrats are introducing the #RaiseTheWage Act to give roughly 40 million workers a raise. https://t.co/6fgmjy5ORC |
| Rep. Susie Lee | 2019-01-17 13:27:20 | 23 | My fellow new Members & I were elected to work. But @SenateMajLdr McConnell seems to think otherwise. After 27 days, he still hasn’t allowed a single vote in the Senate on multiple House plans to re-open government. RT if you think he’s wrong! #EndTheShutdown #WheresMitch |
| Rep. Scott Peters | 2019-01-12 14:59:00 | 13 | .@realDonaldTrump claims there’s a crisis on our border. The only crisis I see is one he created – and one he must end. #TrumpShutdown #EndTheShutdown https://t.co/2G8wC9Y4ll |
| Raul M. Grijalva | 2019-01-15 12:51:15 | 11 | Americans across the nation are being harmed by the #TrumpShutdown, and Democrats are fighting #ForThePeople to end it. Join us at 11am EST for a hearing on the damage to Indian Country, our #PublicLands & the environment. Watch live at https://t.co/iUX0pXx4iw |
| House Appropriations | 2019-01-17 18:31:06 | 7 | JUST IN: The House passed H.J. Res 28, which would reopen the government TODAY, fund federal agencies through 2/28, get federal employees back to work, & end the chaos of the #TrumpShutdown. This is the 9th bill that @HouseDemocrats have brought up to #EndTheShutdown. https://t.co/Q3UrMlsDqq |
| Ways & Means Committee | 2019-01-11 15:14:45 | 6 | #PresidentTrump: who’s going to pay for the wall?? – – Answer: hurricane victims, apparently. https://t.co/VQnaiJvAIU |
| Nancy Pelosi | 2019-01-11 19:43:41 | 6 | Air traffic controllers have incredibly stressful jobs keeping us safe – but federal ATCs aren’t getting paid during the shutdown. The House already voted to fund the FAA this week. It’s time for the Senate to do the same. #EndTheShutdown https://t.co/9qyX9RV6np |
| Rep. Barbara Lee | 2019-01-15 18:52:31 | 6 | We have the largest pro-choice majority in congressional history – and we will be BOLD in our work to defend women’s reproductive rights. – – With more women than ever in Congress, this is a new era for the @ProChoiceCaucus! https://t.co/38s0gH98vh |
| Senate Democrats | 2019-01-15 15:50:22 | 5 | You must have the integrity, the strength, and the fortitude to tell the president no, regardless of the consequences. – – In short, he must be willing to defend the independence of the Justice Department. – – —@SenFeinstein to Trump Attorney General nominee William Barr https://t.co/zxhgy1EkaC |
knitr::kable(
data.frame(
Name = mostpopRT.rep$name,
Created = mostpopRT.rep$created_at,
Rep_Retweets = mostpopRT.rep$n,
Text = mostpopRT.rep$text,
stringsAsFactors = FALSE
),
caption = "Most Popular Retweets Among Republicans"
)| Name | Created | Rep_Retweets | Text |
|---|---|---|---|
| House Republicans | 2019-01-15 21:25:47 | 7 | Democrats of 2019, meet Democrats of 2014. You two have a lot to talk about… https://t.co/0xoO5Ux5uB |
| Sec. Kirstjen Nielsen | 2019-01-16 19:25:57 | 5 | The Department of Homeland Security and the US Secret Service are fully prepared to support and secure the State of the Union. We thank the Service for their mission focus and dedication and for all they do each day to secure our homeland. |
| Donald J. Trump | 2019-01-11 02:43:00 | 4 | When I took the Oath of Office…. https://t.co/GDhIqteKpv |
| HouseVetAffairsGOP | 2019-01-15 16:20:30 | 4 | This is great news for our #veterans! We must continue to improve and increase economic opportunities for our nation’s servicemembers. https://t.co/lEVAJIJIX1 |
| Senate Republicans | 2019-01-10 17:37:20 | 3 | Why do we need border security? This is why. These are the facts. #SecureOurBorder https://t.co/dMpYb3p4Wy |
| Donald J. Trump | 2019-01-10 22:41:01 | 3 | From the Southern Border…. https://t.co/Vgsf5nEZUH |
| The White House | 2019-01-14 20:15:02 | 3 | Another stabbing at the hands of MS-13 that could have been prevented. Enough is enough. Congress, close this loophole! – – https://t.co/Y9NlVcYfWd |
| Mark Meadows | 2019-01-14 21:31:08 | 3 | Democrats were at $0 for wall funding 3 weeks ago. Today? They’re still at $0. – – The notion that negotiations have stalled is a myth because Dems have not negotiated to start with. When Nancy Pelosi and Chuck Schumer get serious about compromising, we can solve this impasse. |
| House Freedom Caucus | 2019-01-15 18:36:03 | 3 | TUNE IN: This afternoon after votes for HFC special order on wall funding - online at https://t.co/VvRtVFgP2U - approx. 3:45 to 4:15 ET start time #BuildTheWall #DoWhatWeSaid |
| Steve Scalise | 2019-01-16 15:05:23 | 3 | #ShutdownNancy shut down the government, and now #SOTU. What are Democrats afraid of Americans hearing? That 17,000+ criminals were caught last year at the border? 90% of heroin in the US comes across the southern border? Illegal border crossings dropped 90%+ in areas w/ a wall? https://t.co/PzIQ3kxapM |
Note: this metric is of dubious value. See documentation.
mostreach.dem <- most_reach(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'democrat'"
)
mostreach.rep <- most_reach(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'republican'"
)
# For nice Markdown rendering
mostreach.dem$description <- gsub("\n"," -- ",mostreach.dem$description)
mostreach.dem$location <- gsub("\n"," -- ",mostreach.dem$location)
mostreach.dem$text <- gsub("\n"," -- ",mostreach.dem$text)
mostreach.dem$name <- gsub("\n"," -- ",mostreach.dem$name)
mostreach.rep$description <- gsub("\n"," -- ",mostreach.rep$description)
mostreach.rep$location <- gsub("\n"," -- ",mostreach.rep$location)
mostreach.rep$text <- gsub("\n"," -- ",mostreach.rep$text)
mostreach.rep$name <- gsub("\n"," -- ",mostreach.rep$name)
knitr::kable(
data.frame(
Name = mostreach.dem$name,
Created = mostreach.dem$created_at,
Reach = mostreach.dem$n,
Text = mostreach.dem$text,
stringsAsFactors = FALSE
),
caption = "Democrat Tweets with Most 'Reach'"
)| Name | Created | Reach | Text |
|---|---|---|---|
| Bernie Sanders | 2019-01-17 15:08:46 | 8040406 | Yesterday Trump’s nominee to lead the EPA, Andrew Wheeler, wouldn’t call climate change a crisis. And in his opening statement, he didn’t mention the words “climate change” once. – – We need an administration that leads the world in combatting climate change, not denies this crisis. https://t.co/5BFSL80whl |
| Bernie Sanders | 2019-01-11 17:07:11 | 8035021 | Air traffic controllers got paychecks for a total of $0 today because of Trump’s government shutdown. – – This is a crisis! – – Mr. Trump, stop holding our federal employees hostage as political pawns. Open the government now. https://t.co/0YvxmsyWwh |
| Bernie Sanders | 2019-01-11 17:31:00 | 8035021 | Last year, the five largest drug companies in America made nearly $60 billion in profits. The top five pharma CEOs made over $113 million in total compensation in 2017. – – Meanwhile, 1 in 5 Americans can’t afford their prescriptions. That should outrage every member of Congress. |
| Bernie Sanders | 2019-01-11 19:13:39 | 8035021 | Things that are national emergencies: – -30 million without health insurance – -15 million children in poverty – -Tens of thousands dying from opioids and gun violence – -Climate change – – Things that are not national emergencies: – -Building a border wall to satisfy Fox News |
| Bernie Sanders | 2019-01-11 20:30:00 | 8035021 | There’s no crisis at the border but Trump’s shutdown is creating a crisis: The safety of the nation’s air travel system is at risk, tax refunds might not go out on time, national parks are overflowing with trash and hundreds of thousands of federal employees aren’t getting paid. https://t.co/brVbH5VB0F |
| Bernie Sanders | 2019-01-12 18:48:49 | 8035021 | Today, the 3 wealthiest Americans own more wealth than the bottom half of our people, and income and wealth inequality is worse now than at any time since the 1920s. This is a moral outrage and bad economics. Not acceptable. |
| Bernie Sanders | 2019-01-12 20:21:10 | 8035021 | Today, the top 0.1% owns more wealth than the bottom 90%. – – Uncontrolled greed cannot be the driving force behind our economy. – – Our job: demand that all Americans have good paying jobs, health care, quality education, and a healthy environment. |
| Bernie Sanders | 2019-01-13 17:35:53 | 8035021 | Do Trump and his billionaire friends understand that many federal employees live paycheck to paycheck? They have kids to feed and mortgages to pay. – – Mr. President. Grow up. End the shutdown you created. Pay federal employees. |
| Bernie Sanders | 2019-01-13 20:42:32 | 8035021 | Mr. President. Do you want to know what a real “national emergency” is? The scientists tell us that if we don’t combat climate change aggressively, the severe damage done to our country and planet will be irreversible. Now that’s a “crisis.” |
| Bernie Sanders | 2019-01-14 13:47:48 | 8035021 | The federal minimum wage of $7.25 is a starvation wage. That is why I, along with many other members of Congress, will introduce legislation this week to raise that wage to $15 an hour. If you work 40 hours a week, you should not live in poverty. |
knitr::kable(
data.frame(
Name = mostreach.rep$name,
Created = mostreach.rep$created_at,
Reach = mostreach.rep$n,
Text = mostreach.rep$text,
stringsAsFactors = FALSE
),
caption = "Republican Tweets with Most 'Reach'"
)| Name | Created | Reach | Text |
|---|---|---|---|
| Donald J. Trump | 2019-01-11 02:43:00 | 25179607 | When I took the Oath of Office…. https://t.co/GDhIqteKpv |
| Donald J. Trump | 2019-01-11 23:00:23 | 24898995 | I look forward to hosting, right out of the great State of South Carolina, the 2019 NCAA Football Champion Clemson Tigers at the White House on Monday, January 14th. What a game, what a coach, what a team! |
| Donald J. Trump | 2019-01-15 15:18:30 | 24898995 | Congratulations @ClemsonFB! https://t.co/w8viax0OWY |
| Donald J. Trump | 2019-01-11 01:10:49 | 24889150 | Dear Diary… https://t.co/NAuMaQW6fl |
| Donald J. Trump | 2019-01-11 23:29:48 | 24862842 | .@CNN called a San Diego news station (@KUSINews) for negative reports on the Wall. When the station said that Walls work, CNN no longer had interest. #FakeNews https://t.co/IDyXqmDsPq |
| Donald J. Trump | 2019-01-12 14:28:14 | 24862419 | Democrats should come back to Washington and work to end the Shutdown, while at the same time ending the horrible humanitarian crisis at our Southern Border. I am in the White House waiting for you! |
| The White House | 2019-01-14 18:45:17 | 24861937 | President Trump Delivers Remarks at the American Farm Bureau Federation’s 100th Annual Convention https://t.co/tMWj5zj3K0 |
| Donald J. Trump | 2019-01-11 12:05:00 | 24843929 | I often said during rallies, with little variation, that “Mexico will pay for the Wall.” We have just signed a great new Trade Deal with Mexico. It is Billions of Dollars a year better than the very bad NAFTA deal which it replaces. The difference pays for Wall many times over! |
| Donald J. Trump | 2019-01-11 12:40:30 | 24843929 | H1-B holders in the United States can rest assured that changes are soon coming which will bring both simplicity and certainty to your stay, including a potential path to citizenship. We want to encourage talented and highly skilled people to pursue career options in the U.S. |
| Donald J. Trump | 2019-01-11 16:04:25 | 24843929 | Humanitarian Crisis at our Southern Border. I just got back and it is a far worse situation than almost anyone would understand, an invasion! I have been there numerous times - The Democrats, Cryin’ Chuck and Nancy don’t know how bad and dangerous it is for our ENTIRE COUNTRY…. |
wordcloud_plot(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'democrat'",
caption = "Democrats"
)
wordcloud_plot(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'republican'",
caption = "Republicans"
)Note these are saved to files.
sentiment_plots(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'democrat'",
file.name.prefix = "Dem",
caption = "Democrats"
)
#> png
#> 2
sentiment_plots(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'republican'",
file.name.prefix = "Rep",
caption = "Republicans"
)
#> png
#> 2|
|
|
| Democrat Absolute Sentiment | Republican Absolute Sentiment |
|
|
|
| Democrat Relative Sentiment | Republican Relative Sentiment |
timeplot(
con,
start.date = Sys.Date()-7,
where.criteria = "query_users.party = 'republican' OR query_users.party = 'democrat'",
group.column = "query_users.party"
)
#> Warning: Removed 5 rows containing missing values (geom_path).# Import igraph
library(igraph)
# Custom database queries and graph creation.
edgelist.query <- "SELECT query_users_fo.screen_name as follower, query_users_fr.screen_name as friend, query_users_fo.party as follower_party, query_users_fr.party as friend_party FROM query_users as query_users_fo JOIN followers ON query_users_fo.user_id = followers.follower_id JOIN query_users AS query_users_fr ON followers.friend_id = query_users_fr.user_id;"
edgelist.df <- DBI::dbGetQuery(con,edgelist.query)
g <- graph_from_data_frame(edgelist.df)
vertices.df <- data.frame(index=as.integer(V(g)), screen_name = names(V(g)),stringsAsFactors = FALSE)
vertex.query <- sprintf("SELECT query_users.screen_name,query_users.party,query_users.body,user.name,user.location,user.description,user.followers_count,user.statuses_count FROM query_users JOIN user ON query_users.user_id = user.id WHERE query_users.screen_name IN (%s);",paste("'",vertices.df$screen_name,"'",sep="",collapse=","))
vertex.query.df <- DBI::dbGetQuery(con,vertex.query)
vertex.query.df <- vertex.query.df[match(vertices.df$screen_name,vertex.query.df$screen_name),]
bluered <- function(party){
if(tolower(party)=="democrat"){
return("blue")
} else if(tolower(party)=="republican"){
return("red")
} else {
return("black")
}
}
vertex.query.df$color <- sapply(vertex.query.df$party,bluered) ec <- centr_eigen(g,directed=FALSE)
df <- data.frame(
Screen_Name = paste(
"@",
vertex.query.df$screen_name,
sep=""
),
Eig_Centrality = as.numeric(ec$vector)
)
df <- df[order(df[,2],decreasing=TRUE),]
knitr::kable(
df[1:10,],
row.names = FALSE,
caption = "Friend Network Eigenvector Centrality"
)| Screen_Name | Eig_Centrality |
|---|---|
| @SenatorSinema | 1.0000000 |
| @repmarkpocan | 0.9543510 |
| @ErikPaulsen | 0.9501537 |
| @CongressmanGT | 0.9444394 |
| @SenDeanHeller | 0.9217849 |
| @RepLowenthal | 0.9168418 |
| @RepCartwright | 0.8996038 |
| @RepTedLieu | 0.8712734 |
| @RepShimkus | 0.8572330 |
| @RepGaramendi | 0.8517486 |
bc <- centr_betw(g,directed=FALSE)
df <- data.frame(
Screen_Name = paste(
"@",
vertex.query.df$screen_name,
sep=""
),
Bet_Centrality = as.numeric(bc$res)
)
df <- df[order(df[,2],decreasing=TRUE),]
knitr::kable(
df[1:10,],
row.names = FALSE,
caption = "Friend Network Betweenness Centrality"
)| Screen_Name | Bet_Centrality |
|---|---|
| @repmarkpocan | 2669.269 |
| @SenatorSinema | 1962.120 |
| @SenDeanHeller | 1893.394 |
| @ErikPaulsen | 1736.826 |
| @RepTedLieu | 1645.586 |
| @MarkWarner | 1590.546 |
| @RepCartwright | 1555.104 |
| @Call_Me_Dutch | 1501.531 |
| @RepLowenthal | 1389.874 |
| @CongressmanGT | 1367.497 |
clust.labelprop <- cluster_label_prop(as.undirected(g))
clust.labelprop
#> IGRAPH clustering label propagation, groups: 2, mod: 0.31
#> + groups:
#> $`1`
#> [1] "RepDLesko" "RepRalphNorman" "RepTomGarrett"
#> [4] "RepArrington" "RepJimBanks" "RepGallagher"
#> [7] "DrNealDunnFL2" "RepJohnFaso" "RepJackBergman"
#> [10] "RepBrianMast" "RepTrey" "RepPaulMitchell"
#> [13] "RepDrewFerguson" "housebudgetGOP" "amashoffice"
#> [16] "WarrenDavidson" "RepLaHood" "HSBCgop"
#> [19] "RepMiaLove" "CongressmanHice" "RepBuddyCarter"
#> [22] "RepJohnKatko" "RepRussell" "RepMarkWalker"
#> [25] "RepRodBlum" "RepRickAllen" "HurdOnTheHill"
#> + ... omitted several groups/verticesverts <- data.frame(
name = vertex.query.df$screen_name,
label = vertex.query.df$screen_name,
title = vertex.query.df$name,
color = vertex.query.df$color,
stringsAsFactors = FALSE
)
for(n in names(verts)){
g <- set_vertex_attr(g,n,value=verts[,n])
}
plot(g,vertex.color=vertex_attr(g,"color"),vertex.size=4,vertex.label=NA)This is essentially the same analysis as above, but using the user mention network.
usermention.query <- sprintf("SELECT query_users_mentioner.screen_name as mentioner,query_users_mentioned.screen_name as mentioned, status.id as status_id, status.text as status_text, status.created_at as created_at FROM query_users as query_users_mentioner JOIN status ON query_users_mentioner.user_id = status.user_id JOIN user_mention ON status.id = user_mention.status_id JOIN query_users as query_users_mentioned ON user_mention.user_mention_id = query_users_mentioned.user_id WHERE status.created_at > '%s';",format(Sys.Date()-7))
usermentions.df <- DBI::dbGetQuery(con,usermention.query)
if(nrow(usermentions.df) > 0){
g <- graph_from_data_frame(usermentions.df)
vertices.df <- data.frame(index=as.integer(V(g)), screen_name = names(V(g)),stringsAsFactors = FALSE)
vertex.query <- sprintf("SELECT query_users.screen_name,query_users.party,query_users.body,user.name,user.location,user.description,user.followers_count,user.statuses_count FROM query_users JOIN user ON query_users.user_id = user.id WHERE query_users.screen_name IN (%s);",paste("'",vertices.df$screen_name,"'",sep="",collapse=","))
vertex.query.df <- DBI::dbGetQuery(con,vertex.query)
vertex.query.df <- vertex.query.df[match(vertices.df$screen_name,vertex.query.df$screen_name),]
bluered <- function(party){
if(tolower(party)=="democrat"){
return("blue")
} else if(tolower(party)=="republican"){
return("red")
} else {
return("black")
}
}
vertex.query.df$color <- sapply(vertex.query.df$party,bluered)
}if(nrow(usermentions.df) > 0){
ec <- centr_eigen(g,directed=FALSE)
df <- data.frame(
Screen_Name = paste(
"@",
vertex.query.df$screen_name,
sep=""
),
Eig_Centrality = as.numeric(ec$vector)
)
df <- df[order(df[,2],decreasing=TRUE),]
knitr::kable(
df[1:10,],
row.names = FALSE,
caption = "User Mention Network Eigenvector Centrality"
)
}| Screen_Name | Eig_Centrality |
|---|---|
| @senatemajldr | 1.0000000 |
| @EPWDems | 0.7015429 |
| @HouseDemocrats | 0.6601116 |
| @SenWhitehouse | 0.6055886 |
| @SenatorCarper | 0.5006413 |
| @POTUS | 0.4396366 |
| @LeaderHoyer | 0.4281604 |
| @RepEscobar | 0.4143127 |
| @McConnellPress | 0.3965211 |
| @RepDonBeyer | 0.3583638 |
if(nrow(usermentions.df) > 0){
bc <- centr_betw(g,directed=FALSE)
df <- data.frame(
Screen_Name = paste(
"@",
vertex.query.df$screen_name,
sep=""
),
Bet_Centrality = as.numeric(as.numeric(bc$res))
)
df <- df[order(df[,2],decreasing=TRUE),]
knitr::kable(
df[1:10,],
caption = "User Mention Network Betweenness Centrality"
)
}| Screen_Name | Bet_Centrality | |
|---|---|---|
| 384 | @POTUS | 29872.049 |
| 387 | @senatemajldr | 15976.567 |
| 9 | @HouseDemocrats | 13330.200 |
| 180 | @SpeakerPelosi | 11359.181 |
| 45 | @RepBonnie | 4120.114 |
| 198 | @BobbyScott | 3381.458 |
| 24 | @SenWhitehouse | 3334.584 |
| 29 | @RepDonBeyer | 2906.780 |
| 86 | @LeaderHoyer | 2739.351 |
| 202 | @GOPLeader | 2666.236 |
if(nrow(usermentions.df) > 0){
verts <- data.frame(
name = vertex.query.df$screen_name,
label = vertex.query.df$screen_name,
title = vertex.query.df$name,
color = vertex.query.df$color,
stringsAsFactors = FALSE
)
for(n in names(verts)){
g <- set_vertex_attr(g,n,value=verts[,n])
}
plot(
g,
vertex.color=vertex_attr(g,"color"),
vertex.size=4,
vertex.label=NA
)
}This is a final network model, using the retweet network.
retweet.query <- sprintf("SELECT query_users_retweeter.screen_name as retweeter,query_users_retweeted.screen_name as retweeted, status_retweet.id as status_id, status_retweet.text as retweet_text, status_original.text as original_text, status_retweet.created_at as created_at FROM query_users as query_users_retweeter JOIN status as status_retweet ON query_users_retweeter.user_id = status_retweet.user_id JOIN status as status_original ON status_retweet.retweet_status_id = status_original.id JOIN query_users as query_users_retweeted ON status_original.user_id = query_users_retweeted.user_id WHERE status_retweet.created_at > '%s' AND status_retweet.retweet = 1;",format(Sys.Date()-7))
retweets.df <- DBI::dbGetQuery(con,retweet.query)
if(nrow(retweets.df) > 0){
g <- graph_from_data_frame(retweets.df)
vertices.df <- data.frame(index=as.integer(V(g)), screen_name = names(V(g)),stringsAsFactors = FALSE)
vertex.query <- sprintf("SELECT query_users.screen_name,query_users.party,query_users.body,user.name,user.location,user.description,user.followers_count,user.statuses_count FROM query_users JOIN user ON query_users.user_id = user.id WHERE query_users.screen_name IN (%s);",paste("'",vertices.df$screen_name,"'",sep="",collapse=","))
vertex.query.df <- DBI::dbGetQuery(con,vertex.query)
vertex.query.df <- vertex.query.df[match(vertices.df$screen_name,vertex.query.df$screen_name),]
bluered <- function(party){
if(tolower(party)=="democrat"){
return("blue")
} else if(tolower(party)=="republican"){
return("red")
} else {
return("black")
}
}
vertex.query.df$color <- sapply(vertex.query.df$party,bluered)
}if(nrow(retweets.df) > 0){
ec <- centr_eigen(g,directed=FALSE)
df <- data.frame(
Screen_Name = paste(
"@",
vertex.query.df$screen_name,
sep=""
),
Eig_Centrality = as.numeric(ec$vector)
)
df <- df[order(df[,2],decreasing=TRUE),]
knitr::kable(
df[1:10,],
caption = "Retweet Network Eigenvector Centrality"
)
}| Screen_Name | Eig_Centrality | |
|---|---|---|
| 57 | @RepDwightEvans | 1.0000000 |
| 59 | @HouseDemocrats | 0.0544417 |
| 64 | @SpeakerPelosi | 0.0541438 |
| 33 | @RepMGS | 0.0531204 |
| 31 | @RepSusanWild | 0.0465067 |
| 37 | @RepBonnie | 0.0445536 |
| 225 | @RepJuanVargas | 0.0395377 |
| 45 | @RepSwalwell | 0.0174027 |
| 30 | @RepEscobar | 0.0131779 |
| 80 | @BobbyScott | 0.0126534 |
if(nrow(retweets.df) > 0){
bc <- centr_betw(g,directed=FALSE)
df <- data.frame(
Screen_Name = paste(
"@",
vertex.query.df$screen_name,
sep=""
),
Bet_Centrality = as.numeric(bc$res)
)
df <- df[order(df[,2],decreasing=TRUE),]
knitr::kable(
df[1:10,],
caption = "Retweet Network Betweenness Centrality"
)
}| Screen_Name | Bet_Centrality | |
|---|---|---|
| 132 | @RepSusieLee | 7011.030 |
| 152 | @RepBeatty | 6674.646 |
| 49 | @RepYvetteClarke | 6357.756 |
| 116 | @GOPLeader | 6339.321 |
| 18 | @SenatorTimScott | 6237.756 |
| 2 | @carloslcurbelo | 6179.756 |
| 69 | @SenateAgDems | 5540.677 |
| 91 | @RepByrne | 5286.244 |
| 200 | @SenatorCantwell | 5228.244 |
| 123 | @SenatorWicker | 5111.244 |
if(nrow(retweets.df) > 0){
verts <- data.frame(
name = vertex.query.df$screen_name,
label = vertex.query.df$screen_name,
title = vertex.query.df$name,
color = vertex.query.df$color,
stringsAsFactors = FALSE
)
for(n in names(verts)){
g <- set_vertex_attr(g,n,value=verts[,n])
}
plot(g,vertex.color=vertex_attr(g,"color"),vertex.size=4,vertex.label=NA)
}There are many more interesting analyses that could be done with this set. For example, do how to democrats’ and republicans’ sentiments vary when they use different hashtags or user mentions? Can we classify users as democrat or republican by their Twitter timelines? Hopefully, this package provides a useful toolset in collecting and analyzing Twitter data to answer questions like these.